package com.wbs.shop.nuby.view.activity;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.smtt.sdk.QbSdk;
import com.wbs.shop.nuby.R;
import com.wbs.shop.nuby.api.RxHelper;
import com.wbs.shop.nuby.commom.Key;
import com.wbs.shop.nuby.helper.OrientationEventLifecycle;
import com.wbs.shop.nuby.utils.Logger;
import com.wbs.shop.nuby.view.base.BaseActivity;
import com.wbs.shop.nuby.view.listener.IWebView;
import com.wbs.shop.nuby.view.listener.OnReloadListener;
import com.wbs.shop.nuby.view.listener.OnTitleChangListener;
import com.wbs.shop.nuby.widget.TitleView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by zhoucl on 2018/5/21 0021.
 */
public class WebViewActivity extends BaseActivity implements OnTitleChangListener, View.OnClickListener, OnReloadListener {
    @BindView(R.id.webView)
    IWebView webView;
    @BindView(R.id.titleView)
    TitleView titleView;
    private Disposable mSubscribe;
    private boolean isCancle = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().setFormat(PixelFormat.TRANSLUCENT);//（这个对宿主没什么影响，建议声明）
        super.onCreate(savedInstanceState);
        OrientationEventLifecycle.orientationEventListener(this, this);
        if (QbSdk.isTbsCoreInited()) {
            init();
        } else {
            setContentView(R.layout.view_loading);
            mSubscribe = Observable.timer(5, TimeUnit.SECONDS)
                    .compose(RxHelper.ioMain())
                    .subscribe(new Consumer<Long>() {
                        @Override
                        public void accept(Long aLong) throws Exception {
                            isCancle = true;
                            init();
                        }
                    });
            initTbs();
        }
    }

    private void initTbs() {
        QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {

            @Override
            public void onCoreInitFinished() {

            }

            @Override
            public void onViewInitFinished(boolean init) {
                Logger.e("init==>" + init);
                if (!isCancle)
                    init();
            }
        });
    }

    public void init() {
        setContentView(R.layout.activity_webview);
        webView.setOnTitleChangListener(this);
        Bundle extras = getIntent().getExtras();
        String url = extras.getString(Key.URL);
        Logger.e("url===>" + url);
        webView.loadUrl(url);
        titleView.setNavigationBackListener(this);
    }

    @Override
    public void onTitleChang(String title) {
        if (titleView != null)
            titleView.setTitle(title);
    }

    @Override
    public void onClick(View v) {
        if (webView.canGoBack())
            webView.goBack();
        else
            finish();
    }

    @Override
    public void onReload() {
        webView.reload();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (webView != null) {
            webView.onNewIntent(intent);
        }
    }
//
//    @Override
//    protected void onPause() {
//        super.onPause();
//        if (webView != null) {
//            webView.pauseTimers();
//            webView.onHide();
//        }
//    }
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//        if (webView != null) {
//            webView.resumeTimers();
//            webView.onShow();
//        }
//    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (webView != null) {
            webView.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onDestroy() {
//        if (webView != null && webView.getUrl() != null && (webView.getUrl().contains(Key.HTML_LOGIN) || webView.getUrl().contains(Key.HTML_ACCOUNT_INFO)))
//            NubyLocalBroadcastManager.sendFinishBroadcastSync();
        if (webView != null)
            webView.destroy();
        super.onDestroy();
        if (mSubscribe != null)
            mSubscribe.dispose();
    }

}
